let arr = [];
let editId = 0;
$("#add").on('click',function (){
    let saveData = $("#Parma").val();
    if( saveData !== ''){
        //处理聊天记录
        $.ajax({
            url: 'api/common/handle',
            dataType: 'json',
            data: {handle:saveData},
            cache: false,
            method:'POST',
            success: function (res) {
                //计算金额
                $.ajax({
                    url: 'api/common/getRes',
                    dataType: 'json',
                    data: {msg: res.data,arr:arr},
                    cache: false,
                    method:'POST',
                    success: function (res) {
                        arr = res.data;
                        console.log(arr);
                        let pd = res.data.pd;
                        let pp = res.data.pp;

                        let total = res.data.total +'元';
                        $("#total").html(total);
                        //陪陪
                        let ppstr = '<div><div>陪陪：</div><ul>';
                        for(let i in pp){
                            ppstr += '<li>' + i  + '：'  + pp[i] + '</li>' ;
                        }
                        ppstr += '</ul></div>';
                        $('#pp').html(ppstr);

                        //派单
                        let pdstr = '<div><div>派单：</div><ul>';
                        for(let k in pd){
                            pdstr  += '<li>' + k + '：'  + pd[k] + '</li>' ;
                        }
                        pdstr +=  '</ul></div>';
                        $('#pd').html(pdstr);
                    }, error: function () {
                        alert('数据错误，请修正后重试！！');
                    }
                });
            }, error: function () {

            }
        });
    }

});
$("#save").on('click',function () {
    if(arr.length !== 0){
        var tall = prompt("请输入名字","晚星❤");
        if (tall != null) {undefined
            $.ajax({
                url: 'api/common/save',
                dataType: 'json',
                data: {arr:arr,month:$("#month").val(),editId:editId,name:tall},
                cache: false,
                method:'POST',
                success: function (res) {
                    alert('保存成功 *^_^*');
                }, error: function () {
                    alert('数据错误，请修正后重试！！');
                }
            });
        } else {undefined

        }
    }
});

function selectChange(){
    let hid = $("#getHistory").val();
    if(hid == 0){
        arr = [];
        $("#total").html('0元');
        $('#pp').html("");
        $('#pd').html("");
    }else {
        $.ajax({
            url: 'api/common/getHistory',
            dataType: 'json',
            data: {id:hid},
            cache: false,
            method:'POST',
            success: function (res) {
                arr = res.data;
                let pd = res.data.pd;
                let pp = res.data.pp;
                $("#month").val(res.data.month);
                editId = hid;
                let total = res.data.total +'元';
                $("#total").html(total);
                //陪陪
                let ppstr = '<div><div>陪陪：</div><ul>';
                for(let i in pp){
                    ppstr += '<li>' + i  + '：'  + pp[i] + '</li>' ;
                }
                ppstr += '</ul></div>';
                $('#pp').html(ppstr);

                //派单
                let pdstr = '<div><div>派单：</div><ul>';
                for(let k in pd){
                    pdstr  += '<li>' + k + '：'  + pd[k] + '</li>' ;
                }
                pdstr +=  '</ul></div>';
                $('#pd').html(pdstr);
            }, error: function () {

            }
        });
    }

}

$("#reset").on('click',function () {
    arr = [];
    $("#total").html('0元');
    $('#pp').html("");
    $('#pd').html("");
});


